package com.fanxin.config;

import com.fanxin.entities.User;
import com.fanxin.mapper.UserMapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;


public class UserRealm extends AuthorizingRealm {

//    private static final String USERNAME="123";
//    private static final String PASSWORD="123";

    @Autowired
    private UserMapper mapper;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token=(UsernamePasswordToken)authenticationToken;
        String username=token.getUsername();
        User user=mapper.queryUserByUsername(username);
        if(user != null && !user.getUsername().equals(username)){
            return null;
        }
//        if(!user.getPassword().equals(password)){
//            return null;
//        }
        return new SimpleAuthenticationInfo(user,user.getPassword(),this.getName());
    }
}
